package com.mine.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mine.pojo.Address;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.service.AddressService;

@Controller
public class AddressController {

	@Autowired
	AddressService addressService;
	//根据token获取某个用户的地址列表
	@RequestMapping(value="/address/list/{token}",method=RequestMethod.GET)
	@ResponseBody
	public Result addOneGoodsToCart(@PathVariable String token){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = addressService.selectAddressList(token);
		return result;
	}
	
	//添加地址
	@RequestMapping(value="/address/add/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result addAddress(@PathVariable String token, @RequestBody Address address){
		if (address==null||token==null||address.getPhone()==null||address.getContact()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		if (address.getPhone().length()!=11) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = addressService.addAddress(token, address);
		return result;
	}
	
	//修改地址
	@RequestMapping(value="/address/update/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateAddress(@PathVariable String token, @RequestBody Address address){
		if (address==null||token==null||address.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = addressService.updateAddress(token, address);
		return result;
	}
	
	//删除地址
	@RequestMapping(value="/address/delete/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result deleteAddressById(@PathVariable String token, @RequestBody Address address){
		if (address==null||token==null||address.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = addressService.deleteAddressById(token, address);
		return result;
	}
	
	//设为默认（优先）地址 priority是否是默认优先的地址 1默认优先 0一般普通地址
	@RequestMapping(value="/address/setPriority/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updatePriorityAddress(@PathVariable String token, @RequestBody Address address){
		if (address==null||token==null||address.getId()==null||address.getPriority()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = addressService.updatePriorityAddress(token, address);
		return result;
	}
	
	//获取默认（优先）地址 priority是否是默认优先的地址 1默认优先 0一般普通地址
	@RequestMapping(value="/address/getPriority/{token}",method=RequestMethod.GET)
	@ResponseBody
	public Result getPriorityAddress(@PathVariable String token){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = addressService.getPriorityAddress(token);
		return result;
	}
}
